import { Injectable } from '@angular/core';
import { TagService } from "./tag.service";
import { Http } from "@angular/http";
import { TagDict } from "../entity/tag-dict.model";
import { Subject } from "rxjs/Rx";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class TagDictService {   
    /**
     * 测点字典
     * 例如
     * constructor(private tagDictService:TagDictService){
     *     let power:Tag = tagDictService.getTag["Power"];
     * }
     * @type {TagDict}
     * @memberof TagDictService
     */
    public getTag: TagDict = {};  

    private tagService: TagService;
    
    constructor(private http: InterceptorService) {     
    
    }
    public initData():Promise<boolean> {   
       this.tagService = new TagService(this.http);         
       return  this.tagService.getTagList()
            .then(res => {
                res.forEach(tag => {
                    this.getTag[tag.tagKey] = tag;
                })
                return  Promise.resolve(true);
            })
            .catch(this.handleError);
  
    }
    private handleError(error: any): Promise<any> {    
        return Promise.reject(error.message || error);
    }

}


// WEBPACK FOOTER //
// ./src/app/service/tag-dict.service.ts